/*
 * $Id: ResourceBundleUtil.java,v 1.2 2009/08/20 21:39:23 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.onwatch.util;

import java.util.Locale;
import java.util.ResourceBundle;

import org.apache.regexp.RE;
import org.apache.regexp.RESyntaxException;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.ASCUtil;
/**
 * 
 * @author 212042946
 * This is a resource bundle utility class. This class allows user to get i18n message
 * with runtime parameters. This class uses Java ResourceBundle package to manage resources.
 */
public class ResourceBundleUtil {
	/**
	 * Get internationalized message from resource bundle
	 * @param baseName	baseName to the message source
	 * @param key	Key to look up the message
	 * @param params	parameter for the message
	 * @param defaultMsg	default message
	 * @param locale	locale
	 * @return message text
	 */
	public static String getMessage(String baseName, 
									String key, 
									Object[] params, 
									String defaultMsg, 
									Locale locale) {
		String message = defaultMsg;
		ResourceBundle bundle = getResourceBundle(baseName, locale);
		if(!ASCUtil.isNullOrEmptyObject(bundle)) {
			String tmpMessage = bundle.getString(key);
			if(!ASCUtil.isNullOrEmpty(tmpMessage)) {
				if((params != null) && (params.length > 0)) {
					try {
						tmpMessage = formatMessage(tmpMessage, params);
						message = tmpMessage;
					} catch (RESyntaxException ree) {
						ASCLogger.warn(ResourceBundleUtil.class, 
								"getMessage", 
								"Get Exception while replacing message with parameter values", 
								ree);
					}
				} else {
					message = tmpMessage;
				}
			}
		}
		return message;
	}

	/**
	 * Get ResourceBundle object according to baseName and locale
	 * @param baseName
	 * @param locale
	 * @return
	 */
	public static ResourceBundle getResourceBundle(String baseName, Locale locale) {
		ResourceBundle bundle = null;
		if(locale == null) {
			bundle = ResourceBundle.getBundle(baseName);
		} else {
			bundle = ResourceBundle.getBundle(baseName, locale);
		}
		return bundle;
	}
	
	/*
	 * Fill parameter to message
	 */
	private static String formatMessage(String message, Object[] params) 
		throws RESyntaxException {
		String newMessage = message;
		for(int idx=0; idx<params.length;idx++) {
			String param = convertToString(params[idx]);
			if(!ASCUtil.isNullOrEmpty(param)) {
				RE checkForVar = new RE("^(.*)\\{" + idx + "\\}(.*)$");
				if(checkForVar.match(newMessage)) {
					newMessage = checkForVar.getParen(1) + param + checkForVar.getParen(2);
				}
			}
		}
		return newMessage;
	}
	
	/*
	 * Convert object type parameter to String
	 */
	private static String convertToString(Object param) {
		String message = null;
		if(param instanceof String) {
			message = (String)param;
		} else if (param instanceof Integer) {
			message = ((Integer)param).toString();
		} else if (param instanceof Long) {
			message = ((Long)param).toString();
		} else if (param instanceof Double) {
			message = ((Double)param).toString();
		} else {
			ASCLogger.warn(ResourceBundleUtil.class, "convertToString", "Unknown object type");
		}
		return message;
	}
}
